package src.component
{
	import flash.display.InteractiveObject;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	/**
	 * 滚动条
	 */
	public class UIScrollBar extends EventDispatcher
	{
		private var _barBack:Sprite;
		private var _barBlock:Sprite;
		private var _upBtn:SimpleButton;
		private var _downBtn:SimpleButton;
		private var _scrollRect:Rectangle;
		private var _maxCount:int;
		//滚动比例
		private var _per:Number = 0;
		//当前滚动点
		private var _index:int = 0;
		private var _totalLength:int = 0;
		//滚动每行的个数
		private var _lineNum:uint = 1; 
		private var _wheelObject:InteractiveObject;
		
		private var _upNum:Number = 0;
		private var _downNum:Number = 0;

		public function UIScrollBar(barBlock:Sprite, barBack:Sprite, maxCount:int, upBtn:SimpleButton = null, downBtn:SimpleButton = null)
		{
			_barBlock = barBlock;
			_barBack = barBack;
			_maxCount = maxCount;
			_upBtn = upBtn;
			_downBtn = downBtn;
			//
			if (_upBtn)
			{
				_upNum = _upBtn.height;
				_upBtn.x = _barBack.x;
				_upBtn.y = _barBack.y;
				_upBtn.mouseEnabled = false;
			}
			if (_downBtn)
			{
				_downNum = _downBtn.height;
				_downBtn.x = _barBack.x;
				_downBtn.y = _barBack.y + _barBack.height - _downBtn.height;
				_downBtn.mouseEnabled = false;
			}
			//
			_barBlock.buttonMode = true;
			_barBlock.mouseEnabled = false;
			upDateScroll();
			_barBlock.y = _scrollRect.y;
			_barBlock.visible = false;
		}

		public function get index():int
		{
			return _index;
		}
		public function get lineNum():int
		{
			return _lineNum;
		}
		public function set lineNum(i:int):void
		{
			_lineNum = i;
		}

		public function set index(v:int):void
		{
			if (v < 0 || v > _totalLength)
			{
				return;
			}
			_index = v;
			_barBlock.y = _index * _per + _scrollRect.y;
			dispatchEvent(new MouseEvent(MouseEvent.MOUSE_MOVE));
		}

		public function set totalLength(len:int):void
		{
			_totalLength = len;
			_barBlock.y = _scrollRect.y;
			_index = 0;
			if (len > _maxCount)
			{
				_barBlock.mouseEnabled = true;
				_barBlock.visible = true;
				_per = _scrollRect.height / (len - _maxCount);
				_barBlock.addEventListener(MouseEvent.MOUSE_DOWN, onBarBlockDown);
				_barBack.addEventListener(MouseEvent.MOUSE_DOWN, onBackDown);
				if (_wheelObject)
				{
					_wheelObject.addEventListener(MouseEvent.MOUSE_WHEEL, onWheel);
				}
				if (_upBtn)
				{
					_upBtn.mouseEnabled = true;
					_upBtn.addEventListener(MouseEvent.MOUSE_DOWN, onUpDown);
				}
				if (_downBtn)
				{
					_downBtn.mouseEnabled = true;
					_downBtn.addEventListener(MouseEvent.MOUSE_DOWN, onDownDown);
				}
			}
			else
			{
				_barBlock.mouseEnabled = false;
				_barBlock.visible = false;
				_barBlock.removeEventListener(MouseEvent.MOUSE_DOWN, onBarBlockDown);
				_barBack.removeEventListener(MouseEvent.MOUSE_DOWN, onBackDown);
				if (_wheelObject)
				{
					_wheelObject.removeEventListener(MouseEvent.MOUSE_WHEEL, onWheel);
				}
				if (_upBtn)
				{
					_upBtn.mouseEnabled = false;
					_upBtn.removeEventListener(MouseEvent.MOUSE_DOWN, onUpDown);
					if(_upBtn.stage)_upBtn.stage.removeEventListener(MouseEvent.MOUSE_UP, onUpUp);
				}
				if (_downBtn)
				{
					_downBtn.mouseEnabled = false;
					_downBtn.removeEventListener(MouseEvent.MOUSE_DOWN, onDownDown);
					if(_downBtn.stage)_downBtn.stage.removeEventListener(MouseEvent.MOUSE_UP, onDownUp);
				}
			}
		}

		public function set wheelObject(o:InteractiveObject):void
		{
			_wheelObject = o;
			if (_barBlock.mouseEnabled)
			{
				_wheelObject.addEventListener(MouseEvent.MOUSE_WHEEL, onWheel);
			}
		}

		public function get wheelObject():InteractiveObject
		{
			return _wheelObject;
		}

		public function destroy():void
		{
			_barBlock.removeEventListener(MouseEvent.MOUSE_DOWN, onBarBlockDown);
			_barBack.removeEventListener(MouseEvent.MOUSE_DOWN, onBackDown);
			if (_wheelObject)
			{
				_wheelObject.removeEventListener(MouseEvent.MOUSE_WHEEL, onWheel);
				_wheelObject = null;
			}
			if (_upBtn)
			{
				_upBtn.removeEventListener(MouseEvent.MOUSE_DOWN, onUpDown);
				if(_upBtn.stage) _upBtn.stage.removeEventListener(MouseEvent.MOUSE_UP, onUpUp);
				_upBtn = null;
			}
			if (_downBtn)
			{
				_downBtn.removeEventListener(MouseEvent.MOUSE_DOWN, onDownDown);
				if(_downBtn.stage) _downBtn.stage.removeEventListener(MouseEvent.MOUSE_UP, onDownUp);
				_downBtn = null;
			}
			_barBlock = null;
			_barBack = null;
			_scrollRect = null;
		}
		
		//--------------------------------------------------------
		// private
		//--------------------------------------------------------
		
		private function upDateScroll():void
		{
			_scrollRect = new Rectangle(_barBack.x - (_barBlock.width - _barBack.width) / 2, _barBack.y + _upNum, 0, _barBack.height - _barBlock.height - _upNum - _downNum);
		}
		
		//--------------------------------------------------------
		// event
		//--------------------------------------------------------

		private function onBarBlockDown(e:MouseEvent):void
		{
			_barBlock.startDrag(false, _scrollRect);
			_barBlock.stage.addEventListener(MouseEvent.MOUSE_MOVE, onBarBlockMove);
			_barBlock.stage.addEventListener(MouseEvent.MOUSE_UP, onBarBlockUp);
			_barBlock.removeEventListener(MouseEvent.MOUSE_DOWN, onBarBlockDown);
		}

		private function onBarBlockMove(e:MouseEvent):void
		{
			if (_barBlock.y < _scrollRect.y)
			{
				_barBlock.y = _scrollRect.y;
			}
			if (_barBlock.y > _scrollRect.bottom)
			{
				_barBlock.y = _scrollRect.bottom;
			}
			
			var index:int = Math.round((_barBlock.y - _scrollRect.y) / _per);
			if (index < 0)
			{
				index = 0;
			}
			if (Math.ceil(index/_lineNum) != _index)
			{
				_index = Math.ceil(index/lineNum);
				dispatchEvent(new MouseEvent(MouseEvent.MOUSE_MOVE));
			}
		}

		private function onBarBlockUp(e:MouseEvent):void
		{
			_barBlock.stopDrag();
			_barBlock.addEventListener(MouseEvent.MOUSE_DOWN, onBarBlockDown);
			_barBlock.stage.removeEventListener(MouseEvent.MOUSE_MOVE, onBarBlockMove);
			_barBlock.stage.removeEventListener(MouseEvent.MOUSE_UP, onBarBlockUp);
		}

		private function onBackDown(e:MouseEvent):void
		{
			_barBlock.y = (_barBack.parent.mouseY - _scrollRect.y) / (_scrollRect.height + _barBlock.height) * _scrollRect.height + _scrollRect.y;
			onBarBlockMove(null);
		}

		/**
		 * 滚轮
		 * @param e
		 *
		 */
		private function onWheel(e:MouseEvent):void
		{
			if (_barBlock.y >= _scrollRect.y && _barBlock.y <= _scrollRect.bottom)
			{
				_barBlock.y -= e.delta;
				onBarBlockMove(null);
			}
		}


		/**
		 * 上
		 * @param e
		 *
		 */
		private function onUpDown(e:MouseEvent):void
		{
			_upBtn.addEventListener(Event.ENTER_FRAME, onUpEnter);
			if(_upBtn.stage)_upBtn.stage.addEventListener(MouseEvent.MOUSE_UP, onUpUp);
		}

		private function onUpUp(e:MouseEvent):void
		{
			if(_upBtn.stage)_upBtn.stage.removeEventListener(MouseEvent.MOUSE_UP, onUpUp);
			_upBtn.removeEventListener(Event.ENTER_FRAME, onUpEnter);
		}

		private function onUpEnter(e:Event):void
		{
			if (_barBlock.y >= _scrollRect.y)
			{
				_barBlock.y -= 3;
				onBarBlockMove(null);
			}
			else
			{
				_barBlock.y = _scrollRect.y;
				_upBtn.removeEventListener(Event.ENTER_FRAME, onUpEnter);
			}
		}

		/**
		 * 下
		 * @param e
		 *
		 */
		private function onDownDown(e:MouseEvent):void
		{
			_downBtn.addEventListener(Event.ENTER_FRAME, onDownEnter);
			if(_downBtn.stage)_downBtn.stage.addEventListener(MouseEvent.MOUSE_UP, onDownUp);
		}

		private function onDownUp(e:MouseEvent):void
		{
			if(_downBtn.stage)_downBtn.stage.removeEventListener(MouseEvent.MOUSE_UP, onDownUp);
			_downBtn.removeEventListener(Event.ENTER_FRAME, onDownEnter);
		}

		private function onDownEnter(e:Event):void
		{
			if (_barBlock.y <= _scrollRect.bottom)
			{
				_barBlock.y += 3;
				onBarBlockMove(null);
			}
			else
			{
				_barBlock.y = _scrollRect.bottom;
				_downBtn.removeEventListener(Event.ENTER_FRAME, onDownEnter);
			}
		}
	}
}